package com.lanvideo.service;

import com.lanvideo.dao.UserFavoriteDao;
import com.lanvideo.entity.UserFavorite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户收藏服务
 */
@Service
public class UserFavoriteService {

    @Autowired
    private UserFavoriteDao userFavoriteDao;

    /**
     * 添加收藏
     */
    public boolean addFavorite(Integer userId, Integer videoId) {
        try {
            int result = userFavoriteDao.addFavorite(userId, videoId);
            return result > 0;
        } catch (Exception e) {
            // 如果已经收藏，返回false
            return false;
        }
    }

    /**
     * 取消收藏
     */
    public boolean removeFavorite(Integer userId, Integer videoId) {
        int result = userFavoriteDao.removeFavorite(userId, videoId);
        return result > 0;
    }

    /**
     * 切换收藏状态
     */
    public boolean toggleFavorite(Integer userId, Integer videoId) {
        if (isFavorited(userId, videoId)) {
            return removeFavorite(userId, videoId);
        } else {
            return addFavorite(userId, videoId);
        }
    }

    /**
     * 检查是否已收藏
     */
    public boolean isFavorited(Integer userId, Integer videoId) {
        return userFavoriteDao.isFavorited(userId, videoId);
    }

    /**
     * 获取用户的收藏列表
     */
    public List<UserFavorite> getUserFavorites(Integer userId) {
        return userFavoriteDao.getUserFavorites(userId);
    }

    /**
     * 获取视频的收藏数量
     */
    public int getVideoFavoriteCount(Integer videoId) {
        return userFavoriteDao.getVideoFavoriteCount(videoId);
    }

    /**
     * 获取用户的收藏数量
     */
    public int getUserFavoriteCount(Integer userId) {
        return userFavoriteDao.getUserFavoriteCount(userId);
    }

    /**
     * 删除收藏
     */
    public boolean deleteFavorite(Integer id) {
        int result = userFavoriteDao.deleteFavorite(id);
        return result > 0;
    }

    /**
     * 根据ID获取收藏
     */
    public UserFavorite getFavoriteById(Integer id) {
        return userFavoriteDao.getFavoriteById(id);
    }
}
